package com.hbrj.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hbrj.entity.Student;
import com.hbrj.mapper.StudentMapper;
import com.hbrj.service.StudentService;

import  com.alibaba.fastjson.JSONObject;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class StudentController {
    @Resource
    StudentService studentService;
    @Resource
    StudentMapper studentMapper;


    //查询所有学生信息
    @RequestMapping("/selectAll")
    @ResponseBody
    public List<User>selectAll() throws JsonProcessingException {
        return studentService.selectAll();
    }
    //查询指定学生信息
    @RequestMapping("/select")
    @ResponseBody
    public Student selectByName(HttpServletRequest request) {
        studentService.select(request.getParameter("name"));
        return studentService.select(request.getParameter("name"));
    }
    //更新指定学生信息
    @RequestMapping("/update")
    @ResponseBody
    public String updateByGrade(@RequestBody  JSONObject jsonObject) throws JSONException {
        JSONObject jo= jsonObject.getJSONObject("data");
        String result=jo.toString();
        Student student =JSONObject.parseObject(result,Student.class);
        studentMapper.updateByPrimaryKey(student);

        return "修改成功！";
    }
    //删除指定学生信息
    @RequestMapping("/delete")
    @ResponseBody
    public String selectByDelete(@RequestBody JSONObject jsonObject) {
        JSONObject jo= jsonObject.getJSONObject("data");
        String result=jo.toString();
        Student student =JSONObject.parseObject(result,Student.class);
        studentService.delete(student.getId());

        return "删除成功";
    }
    //增加学生信息
    @RequestMapping("/add")
    @ResponseBody
    public String addStudent(@RequestBody JSONObject jsonObject) {
        JSONObject jo= jsonObject.getJSONObject("data");
        String result=jo.toString();
        Student student =JSONObject.parseObject(result,Student.class);
        Student student_info =new Student(student.getName(),student.getStuId(),student.getDepart(),student.getGrade(),student.getSex());
        studentMapper.insertSelective(student_info);
        return "添加成功";
    }

}
